<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" href="css/layui.css">
    <script src="layui.js"></script>
</head>

<body>
    <div id="test1"></div>
    <button id="btn" type="button" class="layui-btn" onclick="showCheckNode()">获取选中节点</button>

    <script>
        layui.use('tree', function () {
            var tree = layui.tree;

            var $ = layui.$;
            $.post(
                "http://stuapi.ysdjypt.com/api/GetModulesByRoleIdLayUI"
                , {
                    roleId: '37da0768-4100-4ff3-b9d7-0cc58aae2a7c'
                    , token: '69f84759-5242-488f-b98c-88b8e504dd95'
                }, function (res) { //渲染
                    var inst1 = tree.render({
                        elem: '#test1'  //绑定元素
                        , showCheckbox: true//开启复选框
                        , edit: ['add', 'update', 'del']//开启编辑图标
                        , data: res
                        , click: function (obj) {
                            console.log(obj.data); //得到当前点击的节点数据
                            console.log(obj.state); //得到当前节点的展开状态：open、close、normal
                            console.log(obj.elem); //得到当前节点元素

                            console.log(obj.data.children); //当前节点下是否有子节点
                        }
                        , oncheck: function (obj) {
                            console.log(obj.data); //得到当前点击的节点数据
                            console.log(obj.checked); //得到当前节点的展开状态：open、close、normal
                            console.log(obj.elem); //得到当前节点元素
                        }, operate: function (obj) {
                            var type = obj.type; //得到操作类型：add、edit、del
                            var data = obj.data; //得到当前节点的数据
                            var elem = obj.elem; //得到当前节点元素

                            //Ajax 操作
                            var id = data.id; //得到节点索引
                            if (type === 'add') { //增加节点
                                console.log(data);
                                $.post(
                                    "http://stuapi.ysdjypt.com/api/CreateModule"
                                    , {
                                        name: data.title
                                        , parentId: data.id
                                        , path: data.href
                                        , weight: '1'
                                        , token: '69f84759-5242-488f-b98c-88b8e504dd95'
                                    }
                                    , function (res) {
                                    
                                        layer.msg(res.success);
                                        //console.log(res);
                                    }, 'json'
                                );
                                //返回 key 值
                                return 123;
                            } else if (type === 'update') { //修改节点
                                console.log(data.id);
                                $.post(
                                    "http://stuapi.ysdjypt.com/api/UpdateModule"
                                    , {
                                        mId: data.id
                                        , name: data.title
                                        , parentId: parentId
                                        , path: data.href
                                        , weight: '2'
                                        , token: '69f84759-5242-488f-b98c-88b8e504dd95'
                                    }
                                    , function (res) {
                                        if (res.success) {
                                            layer.msg("修改节点成功");
                                        } else {
                                            layer.msg("修改节点失败");
                                        }
                                        //console.log(res);
                                    }, 'json'
                                );
                                //layer.msg("编辑节点");
                                console.log(elem.find('.layui-tree-txt').html()); //得到修改后的内容
                            } else if (type === 'del') { //删除节点
                                layer.msg("删除节点");
                                $.post(
                                    "http://stuapi.ysdjypt.com/api/DeleteModule"
                                    , {
                                        mId: data.id
                                        , token: '69f84759-5242-488f-b98c-88b8e504dd95'
                                    }
                                    , function (res) {
                                        if (res) {
                                            layer.msg("删除节点成功");
                                        } else {
                                            layer.msg("删除节点失败");
                                        }
                                        //console.log(res);
                                    }, 'json'
                                );
                            };
                        }
                    });
                }, 'json'
            );

            $("#btn").on('click', function () {

                //获得选中的节点
                var checkData = tree.getChecked('demoId');
                for(var i=0;i<=checkData.length;i++){
                    console.log(checkData[i]);
                }
                $.post(
                    "http://stuapi.ysdjypt.com/api/SetSysRights"
                    ,{
                        parentIds:checkData
                        ,rId:'37da0768-4100-4ff3-b9d7-0cc58aae2a7c'
                        ,token: '69f84759-5242-488f-b98c-88b8e504dd95'
                    }
                    ,function(res){
                        layer.msg(res.success);
                    },'json'
                );
                console.log(checkData);
            });



        });
    </script>
</body>

</html>